import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: "/home",
    name: "home",
    component: () => import("../components/Home.vue"),
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: () => import("../components/index/Welcome.vue") },
      { path: "/users", component: () => import("../components/user/Users.vue") },
      { path: "/rights", component: () => import("../components/power/Rights.vue") },
      { path: "/roles", component: () => import("../components/power/Roles.vue") },
      { path: "/categories", component: () => import("../components/goods/Cate.vue") },
      { path: "/params", component: () => import("../components/goods/Params.vue") },
      { path: "/goods", name: "goods", component: () => import("../components/goods/Goods.vue") },
      { path: "/goods/add", component: () => import("../components/goods/Add.vue") },
      { path: "/goods/edit", component: () => import("../components/goods/Edit.vue") },
      { path: "/orders", component: () => import("../components/order/Orders.vue") },
    ],
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    return next();
  }
  const userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
  if (!userInfo) {
    return next("/login");
  }
  next();
});
export default router;
